package com.tiankun.eurekauserinfo.service.impl;

import com.tiankun.eurekauserinfo.exception.BusinessException;
import com.tiankun.eurekauserinfo.mapper.UserMapper;
import com.tiankun.eurekauserinfo.pojo.User;
import com.tiankun.eurekauserinfo.service.UserService;
import com.tiankun.eurekauserinfo.utils.Md5Utils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/11/11
 * Description
 * Version: V1.0
 */
@Service(value = "userService")
@Transactional
public class UserServiceImpl implements UserService {


    @Resource
    UserMapper userMapper;
    User user = new User();






    @Override
    public User findByPhone(String userPhone) {
        this.user.setPhone(userPhone);
        User user = userMapper.selectOne(this.user);

        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        //对密码和盐进行处理，返回给前端空值
        user.setPassword(null);
        user.setSalt("");


        //通过电话查找user
        return user;
    }

    @Override
    public Integer updateUser(User user) {
        if (user.getId() == null) {
            throw new BusinessException("userId不能为空");
        }
//        updateByPrimaryKey：根据主键更新所有属性

//        updateByPrimaryKeySelective：根据主键更新有值属性
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public Integer updatePassword(Integer userId, String password, String newPassword) {
        this.user.setId(userId);
        User user = userMapper.selectOne(this.user);
        //判断用户旧密码是否正确，正确才能修改密码，错误不能操作
        if (user.getPassword().equals(Md5Utils.md5Password(password, user.getSalt()))) {
            //创建一个新的演
            String salt = Md5Utils.createSalt();
            this.user.setSalt(salt);
            //对面进行加密处理
            this.user.setPassword(Md5Utils.md5Password(newPassword, salt));
            //根据主键对有的值进行更改
            return userMapper.updateByPrimaryKeySelective(this.user);
        } else {
            throw new BusinessException("密码错误，修改密码失败");
        }
    }

    @Override
    public Integer findPassword(String phone, String yzm, String newPassword) {

        Example example = new Example(User.class);
        //首先获取验证码
        //通过其他模块获取到验证码的值
        //TODO
        String userYzm = "6666";
        //判断验证码是否正确
        if (yzm.equals(userYzm)) {
            //修改用户密码
            //生成盐
            String salt = Md5Utils.createSalt();
            user.setSalt(salt);
            user.setPassword(Md5Utils.md5Password(newPassword, salt));
            example.createCriteria().andEqualTo("phone", phone);
            //根据给定的example更新有值的属性
            return userMapper.updateByExampleSelective(user, example);
        } else {
            throw new BusinessException("验证码不正确");
        }
    }
}
